package org.syncsoft.overalt.gui.impl.tableDataModel;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.syncsoft.overalt.gui.api.tableDataModel.AbstractTableDataModel;


/**
 * 
 * @author tptfc
 *
 */
@SuppressWarnings("serial")
public class StringTableDataModel extends AbstractTableDataModel {
	private Map<Integer, String> valuesIndex;
	
	{
		setValuesIndex(new HashMap<Integer, String>());
	}
	
	public StringTableDataModel(Collection<String> columnNames, Collection<String> values, Collection<String> selectedValues) {
		setDataVector(createValues(columnNames.size(), values, selectedValues), columnNames.toArray());
	}
	
	
	/**
	 * get row value
	 * @param row
	 * @return
	 */
	public String getValue(int row) {
		return getValuesIndex().get(row);
	}
	
	/**
	 * @precondition communities != null
	 * @postcondition array of objects in format [selected:boolean] [description:String] [code:Integer]
	 * @param values
	 * @param selectedValues
	 * @return
	 */
	public  Object[][] createValues(int columnsCount, Collection<String> values, Collection<String> selectedValues) {
		Object valuesArray[][] = new Object[0][columnsCount];
		if (values != null) {
			valuesArray = new Object[values.size()][columnsCount];
			
			List<String> sortedValues = new ArrayList<String>(values);
			Collections.sort(sortedValues);
			
			int i = 0;
			for (String value: sortedValues) {
				valuesArray[i][0] = selectedValues != null ? selectedValues.contains(value) : false;
				valuesArray[i][1] = value;
				
				i++;
			}
		}
		
		return valuesArray;
	}

	private Map<Integer, String> getValuesIndex() {
		return valuesIndex;
	}

	private void setValuesIndex(Map<Integer, String> valuesIndex) {
		this.valuesIndex = valuesIndex;
	}
}
